package com.ruoyi.project.bi.controller.model;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.bi.domain.common.PageParam;
import com.ruoyi.project.bi.domain.common.Result;
import com.ruoyi.project.bi.domain.model.Cube;
import com.ruoyi.project.bi.service.model.CubeService;
import com.ruoyi.project.bi.util.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/model")
public class CubeController extends BaseController {
	
	@Autowired
	private CubeService cubeService;
	
	@GetMapping("/listCube")
	public AjaxResult list(){
		return AjaxResult.success(cubeService.list());
	}
	
	@RequestMapping(value="/pageCube")
	public AjaxResult page(@RequestParam String key,@RequestParam PageParam page){
		PageHelper.startPage(page.getPage(), page.getRows());
		List<Cube> ls = cubeService.listCube(key);
		PageInfo<Cube> pageInfo=new PageInfo<Cube>(ls);
		return AjaxResult.success(pageInfo);
	}

	@PostMapping("/saveCube")
	public AjaxResult saveCube(@RequestBody Cube cube){
		return cubeService.insertCube(cube);
	}
	
	@PutMapping("/updateCube")
	public AjaxResult updateCube(@RequestBody Cube cube){
		Result ret = cubeService.updateCube(cube);
		return AjaxResult.success(ret);
	}
	
	@DeleteMapping("/delCube/{cubeId}")
	public AjaxResult delCube(@PathVariable Integer cubeId){
		Result ret = cubeService.deleteCube(cubeId);
		return AjaxResult.success(ret);
	}
	
	@GetMapping(value="/getCube")
	public AjaxResult getCube(@RequestParam Integer cubeId){
		JSONObject cube = cubeService.getCubeById(cubeId);
		return AjaxResult.success(cube);
	}
	
	@RequestMapping(value="/treeCube")
	public AjaxResult treeCube(@RequestParam Integer cubeId){
		List<Map<String, Object>> ret = cubeService.treeCube(cubeId);
		if(ret.size() == 0){
			return AjaxResult.error("无数据");
		}
		return AjaxResult.success(ret);
	}
}
